<template>
	<view class="bgg">
		<view  style="height: 200rpx;">
			<view class="info">当前选择生肖：{{shengxiaoDict[animalid]}}</view>
		</view>
		<wheel  @remote="func" ></wheel>
		<view style="height: 100rpx;"></view>
		<skillButton @onkeyclick="onkeyclick" ></skillButton>
		
	</view>
</template>

<script>
	import { linkWebSocket } from "../../component/mod/linkSocket";
	// import yaogan from "../../component/yaogan/yaogan.vue"
	import wheel from "../../component/wheel/wheel.vue"
	import { setTime } from "../../component/mod/setTime"
	import skillButton from "../../component/skill_button/skill_button.vue"
	export default{
		data() {
			return {
				textvalue:0,
				uuid:null,
				animalid:null,
				shengxiaoDict:{
					1:"鼠",2:"牛",3:"虎",4:"兔",5:"龙",6:"蛇",
					7:"马",8:"羊",9:"猴",10:"鸡",11:"狗",12:"猪",
				}
			}
		},
		components: {
			wheel,
			skillButton
		},
		mounted() {
		    window.addEventListener('beforeunload', e => this.logout(e))
		},
		destroyed() {
		    window.removeEventListener('beforeunload', e => this.logout(e))
		},
		onBackPress() {
			this.logout()
		},
		onUnload() {
			this.logout()
		},
		onLoad(options) {
			let that = this;
			if(options["shengxiaoid"] != undefined && options["uuid"] != undefined && options["shengxiaoid"] != null && options["uuid"] != null){
				that.uuid = options.uuid
				that.animalid = parseInt(options.shengxiaoid);
			}else{
				uni.redirectTo({
					url:"/pages/login/login"
				})
			}
			
			linkWebSocket((e)=>{
				//连接成功后 获取socket 对象
				let socket = e.target;
				this.socket = socket;
				// socket.send("连接成功")
				//新建状态
				this.mysend = new setTime(100,this.socket,true)
			})
		},
		methods:{
			logout(e) {
				if(this.uuid == null || this.animalid == null){
					return 
				}
				if(this.socket){
					uni.stopPullDownRefresh()
					try{
						this.mysend.setonkey({"uuid":this.uuid, "shengxiao":this.animalid, "logout":1})
					}catch(err){ 
						console.log(err)
					}
				}
			},
			func(direct){
				if(this.socket){
					uni.stopPullDownRefresh()
					try{
						this.mysend.setdata(this.judgvaue(direct))
					}catch(err){
						// console.log(err)
						this.logout()
					}
				}
			},
			onkeyclick(onkey){
				switch (onkey){
					case "sound":
					case "automove":
					case "jump":
						try{
							this.mysend.setonkey({"uuid":this.uuid, "shengxiao":this.animalid, "option":onkey,"value":-10})
						}catch(err){ 
							console.log(err)
							this.logout()
						}
						break;
					default:
						try{
							let ls = JSON.parse(onkey);
							this.mysend.setonkey({"uuid":this.uuid, "shengxiao":this.animalid, ...ls})
						}catch(err){ 
							console.log(err)
							this.logout()
						}
				}
				
				if(this.socket){
					uni.stopPullDownRefresh()

				}
				
				
				
			},
			judgvaue(str){
				if(this.uuid == null || this.animalid == null){
					uni.redirectTo({
						url:"/pages/login/login"
					})
					return 
				}
				
				let messageobj;
				if(str=='左'){
					messageobj = {"uuid":this.uuid, "shengxiao":this.animalid, "option":'x',"value":-7}
				}else if(str=='右'){
					messageobj = {"uuid":this.uuid, "shengxiao":this.animalid, "option":'x',"value":7}
				}else if(str=='上'){
					messageobj = {"uuid":this.uuid, "shengxiao":this.animalid, "option":'y',"value":-7}
				}else if(str=='下'){
					messageobj = {"uuid":this.uuid, "shengxiao":this.animalid, "option":'y',"value":7}
				}
				
				// else {
				// 	messageobj = {"uuid":this.uuid, "shengxiao":this.animalid, "option":'stop',"value":0}
				// }
				// console.log(str)
				
				return messageobj
			}
		}	
	}
</script>

<style lang="scss" scoped>
.bgg{
	background: url("@/static/1x/资源 背景.png") no-repeat;
	background-size:100% 110%;
	height: 100vh;
	width: 100vw;
	position: absolute;
}
.info{
	margin: 50rpx;
}
</style>